﻿<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        Application("online") = 0
        Dim ServerIP As String = System.Net.Dns.Resolve(Context.Server.MachineName).AddressList(0).ToString
        Dim sql As New MyStringBuilder
        Dim dat As New clsData("connStr")
        Application("myServerIP") = ServerIP
        sql.AppendFormat("update   ServerFarmOnlineCounter set LastChgDate = getdate(), OnlineCounter=  " & Application("online").ToString & " where ServerIP like '" & ServerIP & "%'")
        dat.AccData(sql.ToString)
      
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)

        ' Code that runs on application shutdown

        'Dim ServerIP As String = Application("myServerIP")
        'Dim sql As New MyStringBuilder
        'Dim dat As New clsData("connStr")
        'sql.AppendFormat("delete from  Application_End where ServerIP='" & ServerIP & "'")
        ''MyObj.WriteLog(sql.ToString() & Now.ToString() & "  Application_End ")
        'dat.AccData(sql.ToString)
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
        
        '登入時間6小時
        'Session.Timeout = 3600
        Session.Timeout = 20
        
        
        Application.Lock()
        Application("online") = CType(Application("online"), Integer) + 1
        '對話完畢後，Application解鎖
        Application.UnLock()
        
        Dim ServerIP As String = Application("myServerIP")
        Dim sql As New MyStringBuilder
        Dim dat As New clsData("connStr")
        sql.AppendFormat("update   ServerFarmOnlineCounter set LastChgDate = getdate(), OnlineCounter=  " & Application("online").ToString & " where ServerIP like '" & ServerIP & "%'")
        ' MyObj.WriteLog(sql.ToString() & Now.ToString() & " Session_Start " & Session.Timeout.ToString)
        dat.AccData(sql.ToString)
   
      
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends. 
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer 
        ' or SQLServer, the event is not raised.
        
        
        Application.Lock()
        Application("online") = CType(Application("online"), Integer) - 1
        '對話完畢後，Application解鎖
        Application.UnLock()
        
        Dim ServerIP As String = Application("myServerIP")
        Dim sql As New MyStringBuilder
        Dim dat As New clsData("connStr")
        sql.AppendFormat("update   ServerFarmOnlineCounter set LastChgDate = getdate(), OnlineCounter=  " & Application("online").ToString & " where ServerIP like ='" & ServerIP & "%'")
        ' MyObj.WriteLog(sql.ToString() & Now.ToString() & " Session_End ")
        dat.AccData(sql.ToString)
    End Sub
       
    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
        'MyObj.SetDateLocale("zh-TW")
        MyObj.SetDateLocale("")
       
   
    End Sub
</script>